package com.qyhl.webtv.module_microvideo.shortvideo.theme;

import android.view.MenuItem;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.module_microvideo.R;
import com.qyhl.webtv.module_microvideo.R2;
import com.qyhl.webtv.module_microvideo.shortvideo.theme.fragment.ShortVideoThemeNewFragment;
import com.weavey.loading.lib.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = ARouterPathConstant.SHORT_VIDEO_THEME_NEW)
public class ShortVideoThemeNewActivity extends BaseActivity implements ShortVideoThemeContract.ShortVideoThemeView {
    @BindView(R2.id.toolbar)
    Toolbar toolbar;
    @BindView(R2.id.tabLayout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private String activityId;

    private ShortVideoThemePresenter mPresenter;

    @Override
    protected int getContentView() {
        return R.layout.micro_activity_short_video_theme_new;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarDarkFont(true).titleBar(toolbar).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new ShortVideoThemePresenter(this);

        activityId = getIntent().getStringExtra("actId");

        String title = getIntent().getStringExtra("title");

        toolbar.setTitle(title);

        setSupportActionBar(toolbar);

        mPresenter.getList(activityId);

    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(v -> {
            loadMask.setReloadButtonText("加载中...");
            mPresenter.getList(activityId);
        });
    }

    @Override
    public void setList(List<ShortVideoThemeBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        List<String> nameList = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        nameList.add("全部");
        fragments.add(ShortVideoThemeNewFragment.newInstance(activityId, activityId, true));

        for (int i = 0; i < beanList.size(); i++) {
            nameList.add(beanList.get(i).getName());
            fragments.add(ShortVideoThemeNewFragment.newInstance(beanList.get(i).getId() + "", activityId, false));
        }

        viewPager.setAdapter(new Adapter(getSupportFragmentManager(), fragments, nameList));
        tabLayout.setViewPager(viewPager);

    }

    @Override
    public void setError(String msg) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setErrorText(msg);
        if (msg.contains("网络")) {
            loadMask.setErrorImage(R.drawable.error_network);
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
        }
    }

    static class Adapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        private List<String> names;

        Adapter(FragmentManager fm, List<Fragment> fragmentList, List<String> names) {
            super(fm);
            this.fragmentList = fragmentList;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position);
        }
    }


}
